    #coding=utf-8
import sys

'''
-编写登陆接口
-输入用户名密码
-认证成功后显示欢迎信息
-输错三次后锁定
'''

class bcolors:
    HEADER = '\033[95m'
    OK_BLUE = '\033[94m'
    OK_GREEN = '\033[92m'
    WARING_YELLOW = '\033[93m'
    FAIL = '\033[36m'
    FLASHING = '\033[35m'
    CRITICAL_RED = '\033[31m'
    END = '\033[0m'

def user_login():
    '''
    user login function.
    user intype , if wrong password three times and locked user.
    userfile: user.txt
    '''

    while True:
        name = raw_input(bcolors.WARING_YELLOW + "Please input username: " + bcolors.END).strip()

        if len(name) == 0:
            print bcolors.CRITICAL_RED + "Sorry, you can't intype invaild chart, you can re-try again. " + bcolors.END
            continue
        else:
            #判断用户输入姓名是否正确，只有正确才能继续
            if name != username:
                sys.exit(bcolors.CRITICAL_RED + "Sorry, name is wrong, you can re-run the program !!" + bcolors.END)
            else:
                #用户正确情况下判断用户控制位是否被锁定，true才能正常登陆
                i = 3
                if control == 'true':
                    print bcolors.OK_GREEN + "Congratulations, the name is right, please continue~" + bcolors.END
                    input_pass = raw_input(bcolors.FAIL + "You can input error password three times !\n" + bcolors.END + bcolors.OK_BLUE + "please input your password: " + bcolors.END)

                    while( i >= 1 ):
                        if input_pass == password:
                            print bcolors.OK_GREEN + "Yeah, your are right again~" + bcolors.END
                            print bcolors.OK_GREEN + "More features has developed in future. " + bcolors.END
                            break
                        else:
                            #用户密码输入错误的情况下，循环三次，提示可用次数，超过三次，退出循环
                            i -= 1
                            if i:
                                print bcolors.CRITICAL_RED + "Wrong password, try again. \n" + bcolors.END
                                input_pass = raw_input(bcolors.WARING_YELLOW + "please input your password: " + bcolors.END)
                                continue
                            else:
                                print bcolors.FAIL + "Sorry, the %s user was locked, please contact your administrator." % name + bcolors.END

                                #重新写控制位，表示用户已被锁定
                                temp[2] = "false"
                                new_str =  " ".join(temp)
                                new_file = open('user.txt', 'w')
                                new_file.write(new_str)
                                new_file.close()
                    break
                else:
                    print bcolors.FAIL + "Sorry, your count was locked, please contact your administrator." + bcolors.END
                    break

def user_unlock():
    '''
    判断用户控制位是否是false，false的时候才可以解锁
    '''
    # temp_file = file('user.txt', 'r')
    # temp = temp_file.readline().split()
    name = raw_input(bcolors.WARING_YELLOW + "Please input username: " + bcolors.END)
    if name != username:
        sys.exit(bcolors.CRITICAL_RED + "Sorry, name is wrong, you can re-run the program !!" + bcolors.END)
    else:
        if control == 'true':
            print bcolors.CRITICAL_RED + "The user wasn't locked, you needn't unlock !" + bcolors.END
        else:

            temp[2] = 'true'
            new_str =  " ".join(temp)
            new_file = open('user.txt', 'w')
            new_file.write(new_str)
            new_file.close()
            print bcolors.OK_GREEN + "The user has unlock already, please re-login. " + bcolors.END

def user_exit():
    sys.exit(bcolors.FAIL + "Thank you for useing. Bye-Bye~\n" + bcolors.END)

#user_dict = { 1:'user_login()', 2:'user_unlock()', 3:'user_exit()'}

def more_features():
    print bcolors.FAIL + "More features has developed in future. " + bcolors.END

if __name__ == '__main__':

    #储存用户文件，格式：<user pass flag>
    temp_file = open('user.txt', 'r')
    temp = temp_file.readline().split()

    #三个文件字段分别表示用户名，密码，控制位
    username = temp[0]
    password = temp[1]
    control = temp[2]

    print bcolors.OK_GREEN + '''
=== thanks use sample user control program, you can type ===
++                      1. user login                     ++
++                      2. user unlock                    ++
++                      3. exit program                   ++
++                      x. more features                  ++
    ''' + bcolors.END
    # try:
    choose = raw_input(bcolors.WARING_YELLOW + "please input your choose number: " + bcolors.END)
    if choose == '1':
        print bcolors.OK_GREEN + "~O(∩_∩)O~" + bcolors.END
        user_login()
    elif choose == '2':
        user_unlock()
    elif choose == '3':
        user_exit()
    elif choose == 'x':
        more_features()
    else:
        print bcolors.FAIL + "Sorry, error input, please try again. " + bcolors.END
    # except:
    #     print bcolors.CRITICAL_RED + "your input isn't number, please try again. " + bcolors.END




